package com.example.demoa;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;

public class AddActivity extends AppCompatActivity {
    private EditText edt_s_name;
    private EditText edt_s_age;
    private EditText edt_profession;
    private EditText edt_score;
    private ImageView iv_head;
    private Button btn_add;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        setTitle("添加学生");

        initFindViews();

        initUpdateImg();

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initAddStu();
            }
        });
    }

    private void initAddStu() {
        Student student = new Student();
        student.setName(edt_s_name.getText().toString());
        student.setAge(Integer.parseInt(edt_s_age.getText().toString()));
        student.setProfession(edt_profession.getText().toString());
        student.setScore(Integer.parseInt(edt_score.getText().toString()));
        student.setHeadimg(new BmobFile(new File(path)));
        BmobFile imgFile = new BmobFile(new File(path));
        Log.e("bb", "initAddStu: "+ path +"  "+ imgFile+"  "+ student.getHeadimg().getUrl() );

        Intent  intent = new Intent();
        intent.putExtra("STU", student);
        setResult(2002, intent);
        finish();
    }

    //打开手机系统的相册
    private void initUpdateImg() {
        iv_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2001 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex);  //获取照片路径
            //Log.e("EditActivity", "index = " + columnIndex + " path = " + path );
            cursor.close();

            Bitmap bitmap = BitmapFactory.decodeFile(path);
            iv_head.setImageBitmap(bitmap);
        }
    }

    private void initFindViews() {
        edt_s_name = findViewById(R.id.edt_s_name);
        edt_s_age = findViewById(R.id.edt_s_age);
        edt_profession = findViewById(R.id.edt_profession);
        edt_score = findViewById(R.id.edt_score);
        iv_head = findViewById(R.id.iv_head);
        btn_add = findViewById(R.id.btn_add);
    }
}
